package com.toocms.jinjuhang.ui.index.news

import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.data.system.NewsItemData
import com.toocms.jinjuhang.ui.index.article_details.ArticleDetailsFgt
import com.toocms.jinjuhang.ui.system.ImageItemModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class NewsItemModel(viewModel: NewsModel, val item: ObservableField<NewsItemData>) :
    MultiItemViewModel<NewsModel>(viewModel) {
    val imageItems by lazy {
        val result = ObservableArrayList<ImageItemModel>()
        item.get()?.apply {
            pictures?.forEach { imageItem ->
                result += ImageItemModel(viewModel, pictures, ObservableField(imageItem))
            }
        }
        result
    }
    val imageItemBinding = ItemBinding.of<ImageItemModel>(BR.imageItemModel, R.layout.item_image)

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply {
            val bundle = Bundle()
            bundle.putString(Constants.BundleKey.KEY_NEWS_ID, id)
            bundle.putString(Constants.BundleKey.KEY_TYPE, viewModel.type)
            viewModel.startFragment(ArticleDetailsFgt::class.java, bundle)
        }
    })

    init {
        itemType = Constants.RecyclerViewItemType.ITEM_TYPE_ONE
    }
}